<template>
  <div id="content-item">
    <div class="left">
      <checked-button @click="btnClick" :isChecked="newitem.checked"/>
    </div>
    <div class="center">
      <img :src="item.img" alt="">
    </div>
    <div class="right">
      <div class="title">{{item.title}}</div>
      <div class="desc">{{item.desc}}</div>
      <div>
        <span class="price">￥{{item.price}}</span>
        <span class="count">
          <button @click="decrease">-</button>
          x{{item.count}}
          <button @click="increase">+</button>
        </span>
      </div>
    </div>
  </div>
</template>

<script>
  import CheckedButton from "@/components/content/checkedButton/CheckedButton";
  import {mapGetters} from 'vuex'

  export default {
    name: "CartContentItem",
    components:{
      CheckedButton,
    },
    data(){
      return {
        newitem:this.item
      }
    },
    props:{
      item:{
        type:Object,
        default(){
          return {}
        }
      }
    },
    computed:{
      ...mapGetters(['cartList']),
    },
    methods:{
      btnClick() {
        this.newitem.checked = !this.newitem.checked
        //将newitem传入vuex，再把newitem的checked添加给对应的id商品里面得到状态
        this.$store.dispatch('changeChecked', this.newitem)
      },
      decrease(){
        if(this.newitem.count > 0)
        return this.newitem.count--
      },
      increase(){
        return this.newitem.count++
      },
    }
  }
</script>

<style scoped>
  #content-item{
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 5px 8px;
    border-bottom: 1px solid #ccc;

  }
  .left{
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 10px;
  }
  .center{
    padding: 5px;
  }
  .center img{
    width: 80px;
    height: 100px;
    display: block;
    border-radius: 5px;
  }
  .right{
    overflow: hidden;
    margin: 5px;
    position: relative;
  }
  .right .title,.desc{
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .right .title{
    color: #222222;
  }
  .right .desc{
    margin-top: 8px;
    font-size: 14px;
  }
  .right .price{
    position: absolute;
    left: 0;
    bottom: 0;
    color: #ff5777;
  }
  .right .count{
    position: absolute;
    right: 0;
    bottom: 0;
    font-size: 14px;
  }
  button{
    padding: 0 3px;
    margin:0 2px;
  }
</style>